package top.wardyan.wdev.dbdevassist.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.wardyan.base.model.TableField;
import top.wardyan.base.util.DateUtil;
import top.wardyan.wdev.dbdevassist.model.DetailModel;
import top.wardyan.wdev.dbdevassist.service.IDetailService;

public class DetailServiceImpl<T extends DetailModel<T>> extends top.wardyan.base.service.impl.DetailServiceImpl<T> implements IDetailService<T>{
	
	

	public DetailServiceImpl(T dao) {
		super(dao);
	}
	
	
	
	

	@Override
	public boolean save(T object) {
		if(object != null) {
			
			List<String> tfs = new ArrayList<>();
			List<TableField> ttt = object._tableField();
			//List<Record> rs = Db.use().find("SELECT c.name FROM sys.objects AS b INNER JOIN sys.columns AS c on c.object_id = b.object_id WHERE b.name = ?", object.tableName());
			if(ttt != null) {
				for(TableField r : ttt) {
					tfs.add(r.getField());
				}
			}
			Timestamp now = DateUtil.getCurrentTimestamp();
			if(tfs.contains("sCreateTime") && object.getSCreateTime() == null) {
				object.setSCreateTime(now);
			}
			if(tfs.contains("sLastUpdate") && object.getSLastUpdate() == null) {
				object.setSLastUpdate(now);
			}
			if(tfs.contains("sVersion") && object.getSVersion() < 1) {
				object.setSVersion(1);
			}
			//object.setSVersion(0);
		}
		return super.save(object);
	}





	@Override
	public List<T> list(int mID) {
		return this.List(this.sql + " WHERE mID = ? ORDER BY sIndex ASC", mID);
	}



	@Override
	protected T initPrimaryKey(T obj) {
		if(obj != null && obj.primaryKeyType() > 0) {
			if(obj.primaryKeyType() == 1 && (obj.getDID() == 0)) {
				T t = this.findBySql("SELECT dID FROM " + this.table + " WHERE mID = ? ORDER BY dID DESC", obj.getMID());
				if(t != null) {
					obj.setDID(t.getDID() + 1);
				}else {
					obj.setDID(1);
				}
			}else if(obj.primaryKeyType() == 2 &&  (obj.getDID() == 0)) {
				obj.setDID(getIDService.exec("yp_getid", 0, this.table, 1));
			}
		}
		if(obj != null && obj.getSIndex() == 0) {
			T b = this.findBySql(this.sql + " WHERE mID = ? ORDER BY sIndex DESC", obj.getMID());
			if(b == null || b.getSIndex() < 1) {
				obj.setSIndex(1);
			}else {
				obj.setSIndex(b.getSIndex() + 1);
			}
		}
		return obj;
	}



	@Override
	protected List<T> initPrimaryKey(List<T> objs) {
		return super.initPrimaryKey(objs);
	}



	@Override
	public java.util.List<T> list(String xMainName) {
		return this.List(this.sql + " WHERE xMainName = ?", xMainName);
	}

	@Override
	public T find(int mID, String xField) {
		return this.findBySql(this.sql + " WHERE mID = ? AND xField = ?", mID, xField);
	}
	
	@Override
	public int genDID(int mID) {
		int rs = 0;
		if(dao.primaryKeyType() == 1) {
			T t = this.findBySql("SELECT TOP(1) dID FROM " + this.table + " WHERE mID = ? ORDER BY dID DESC ",  mID);
			if(t != null) {
				rs = t.getDID() + 1;
			}else {
				rs = 1;
			}
		}else if(dao.primaryKeyType() == 2) {
			rs = getIDService.exec(0, this.table, 1);
		}
		return rs;
	}





	@Override
	public int genSIndex(int mID) {
		int rs = 0;
		T t = this.findBySql("SELECT TOP(1) sIndex FROM " + this.table + " WHERE mID = ? ORDER BY dID DESC ", mID );
		if(t != null) {
			rs = t.getSIndex() + 1;
		}else {
			rs = 1;
		}
		return rs;
	}

}
